﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ObjectLibrary;
namespace _0912089_0912172
{
    public class TextField : FormItem
    {

        private char[] _text;
        private int _index = 0;
        private TextureUntity stringTexture;
        private Vector4 _color = Vector4.One;
        private bool _isEditting;


        private char blinkChar ='-';
        private char tempChar ;

        public bool IsEditting
        {
            get { return _isEditting; }
            set { 
                _isEditting = value;
                _text[_index] = tempChar;
                
                ((PlanarModel)_model).Texture = stringTexture.ExportStringTexture(new String(_text));
            }
        }


        public Vector4 Color
        {
            get { return _color; }
            set { 
                _color = value;
                ((PlanarModel)_model).FilterColor = _color;
            }
        }
        public string getString()
        {
            return new string(_text);
        }
        public void  setString(string str,bool resize)
        {
            if (resize)
            {
                _text = str.ToCharArray();
                _index = str.IndexOf(' ');
                if (_index < 0)
                    _index = 0;
                ((PlanarModel)_model).setTexture(stringTexture.ExportStringTexture(str));

                _size = ((PlanarModel)_model).Size;
                ((PlanarModel)_model).FilterColor = Vector4.One;
            }
            else
            {
                int n=str.Length;
                for (int i = 0; i < _text.Length; i++)
                {
                    if (i < n)
                    {
                        _text[i] = str[i];
                    }
                    else
                    {
                        _text[i] = ' ';
                    }
                }
                if (n < _text.Length)
                    _index = n;
                ((PlanarModel)_model).Texture = stringTexture.ExportStringTexture(new string(_text));
            }

            tempChar = _text[_index];
        }
        public TextField( string str, float scale, Vector3 pos, float rotationY)
        {
            _text = str.ToCharArray();
            _index = str.IndexOf(' ');
            if (_index < 0)
                _index = 0;
            tempChar = _text[_index];
            stringTexture = TextureUntity.Create(_content,_graphicsDevice);
            _model = new PlanarModel(_content, stringTexture.ExportStringTexture(str),1, scale, pos, rotationY);
     
            _size = ((PlanarModel)_model).Size;
            Scale = scale;
            RotationY = rotationY;
            Position = pos;
            ((PlanarModel)_model).FilterColor = Vector4.One;
            
        }

        KeyboardState lastKeyState;
        public bool KeyHandle(KeyboardState k)
        {
            if (lastKeyState != null && lastKeyState != k)
            {
                Keys[] key = k.GetPressedKeys();
                if (key.Length != 0 && lastKeyState.IsKeyUp(key[0]))
                {
                    _text[_index] = tempChar;
                    if (key[0] == Keys.Back)
                    {
                        if (_index == 0)
                            return true;
                        for (int i = _index; i < _text.Length; i++)
                        {
                            _text[i - 1] = _text[i];
                        }
                        _text[_text.Length-1] = ' ';
                        _index--;
                    }
                    else if (key[0] == Keys.Enter)
                    {
                        return false;
                    }
                    else if (key[0] == Keys.Delete)
                    {
                        for (int i = _index; i < _text.Length - 1; i++)
                        {
                            _text[i] = _text[i + 1];
                        }
                        _text[_text.Length - 1] = ' ';
                    }
                    else if (key[0] == Keys.Left)
                    {

                        if (_index > 0)
                        {
                            _index--;
                        }

                    }
                    else if (key[0] == Keys.Right)
                    {

                        if (_index < _text.Length - 1)
                        {
                            _index++;
                        }

                    }
                    else
                    {
                        String str = key[0].ToString();
                        if (str.Length == 1)
                        {
                            _text[_index] = str.ElementAt(0);
                            if (_index < _text.Length - 1)
                            {
                                _index++;
                            }

                        }
                    }
                    tempChar = _text[_index];
                }
            }
            
            lastKeyState = k;
            return true;
        }
        private int t = 0;
        
        public override void Update(GameTime gameTime)
        {
            t++;
                if (t > 20)
                    t = 0;
            if (_isEditting)
            {
 
                ((PlanarModel)_model).FilterColor = Vector4.One;
                if (t == 20)
                {
                    _text[_index] = blinkChar;
                }
                else if (t == 10)
                {
                    _text[_index] = tempChar;
                }
                ((PlanarModel)_model).Texture = stringTexture.ExportStringTexture(new String(_text));
            }
            else
            {
                if (isChosen && Enable)
                {
                    ((PlanarModel)_model).FilterColor = _color * (0.5f + (10-t)*(10-t)/200.0f);
                }
                else
                    ((PlanarModel)_model).FilterColor = _color * _defaultLight;
            
            }
        }



    }
}
